import { defineStore } from 'pinia'
import {
  IFoodRecipe, IFoodMaterial, IFoodStep,
  IFoodStats, IFoodRecord
} from '@/types/food'
import {
  addFoodRecipeApi, addFoodRecordApi, deleteFoodRecipeApi, deleteFoodRecordApi,
  queryFoodNameListApi, queryFoodRecipeByIdApi, queryFoodRecipeApi, queryFoodRecordApi,
  queryFoodStatsApi, updateFoodRecipeApi, updateFoodRecordApi, queryFoodCategoryApi
} from '@/apis/food.ts'
import { IDateType, ImagePathEnum } from '@/types'
import type { IDialog } from 'vue3-common/types'
import { IHandleApi } from 'vue3-common/types'
import { ElMessage } from 'element-plus'
import { getCurrentMonth, getCurrentYear, getStartEndDate } from 'vue3-common/utils/dateUtil'
import { isLargeScreen } from '@/utils'
import { isMobile } from 'vue3-common/utils/layoutUtil'

export const useFoodStore = defineStore('food', {
  state: () => ({
    foodRecipeList: [] as IFoodRecipe[],
    currentFoodRecipe: {
      id: 0,
      name: '',
      category: '',
      recommendRate: 0,
      remark: '',
      materialList: [],
      stepList: [],
      recordList: []
    } as IFoodRecipe,
    foodRecordList: [] as IFoodRecord[],
    currentFoodRecord: {
      id: 0,
      name: '',
      category: '',
      date: '',
      person: '',
      imageUrl: ''
    } as IFoodRecord,
    foodNameList: [] as string[],
    queryRecipe: {
      category: ''
    },
    queryRecord: {
      year: getCurrentYear(),
      month: getCurrentMonth()
    },
    queryDateType: 'month' as IDateType,
    queryDateList: [] as string[],
    foodRecipeApiType: 'ADD' as IHandleApi,
    foodRecipeDialog: {
      visible: false,
      title: '',
      data: {}
    } as IDialog<IFoodRecipe>,
    foodMaterialApiType: 'ADD' as IHandleApi,
    foodMaterialDialog: {
      visible: false,
      title: '',
      data: {}
    } as IDialog<IFoodMaterial>,
    editFoodMaterialIndex: 0,
    foodStepApiType: 'ADD' as IHandleApi,
    foodStepDialog: {
      visible: false,
      title: '',
      data: {}
    } as IDialog<IFoodStep>,
    editFoodStepIndex: 0,
    foodRecordApiType: 'ADD' as IHandleApi,
    foodRecordDialog: {
      visible: false,
      title: '',
      data: {}
    } as IDialog<IFoodRecord>,
    editFoodRecordIndex: 0,
    foodImagePath: ImagePathEnum.FOOD,
    rateColorList: ['#E2DEDC', '#F7BA2A', '#009FA8'],
    pageInfo: {
      currentPage: 1,
      pageSize: isLargeScreen() ? 6 : 2,
      totalSize: 1
    },
    isScrollEnd: false,
    foodCategoryList: [] as string[],
    foodStats: {
      recipeCount: 0,
      categoryCount: 0,
      workCount: 0
    } as IFoodStats,
    isRefresh: false
  }),
  actions: {
    async queryFoodRecipeList() {
      const { currentPage, pageSize } = this.pageInfo
      const result = await queryFoodRecipeApi(currentPage, pageSize, this.queryRecipe)
      this.foodRecipeList = result.records
      this.pageInfo.currentPage = result.current
      this.pageInfo.totalSize = result.total
    },
    async queryMobileFoodRecipeList() {
      const { currentPage, pageSize } = this.pageInfo
      const result = await queryFoodRecipeApi(currentPage, pageSize, this.queryRecipe)
      this.foodRecipeList.push(...result.records)
      this.pageInfo.totalSize = result.total
    },
    async queryFoodRecordList() {
      const { year, month } = this.queryRecord
      const date = this.queryDateType === 'year' ? year : month
      this.queryDateList = getStartEndDate(date, this.queryDateType)
      this.foodRecordList = await queryFoodRecordApi(this.queryDateList[0], this.queryDateList[1])
    },
    async queryFoodNameList() {
      this.foodNameList = await queryFoodNameListApi()
    },
    async queryFoodRecipe(id: number) {
      this.currentFoodRecipe = await queryFoodRecipeByIdApi(id)
    },
    async queryFoodStats() {
      this.foodStats = await queryFoodStatsApi()
    },
    async queryFoodCategory() {
      this.foodCategoryList = await queryFoodCategoryApi()
    },
    async handleFoodRecipeApi(id?: number) {
      const { data } = this.foodRecipeDialog
      switch (this.foodRecipeApiType) {
        case 'ADD':
          await addFoodRecipeApi(data)
          ElMessage.success('新增菜谱成功')
          break
        case 'UPDATE':
          await updateFoodRecipeApi(data.id as number, data)
          ElMessage.success('更新菜谱成功')
          break
        case 'DELETE':
          await deleteFoodRecipeApi(id as number)
          ElMessage.success('删除菜谱成功')
          break
        default:
          break
      }
      this.foodRecipeDialog.visible = false
      await this.refreshInfo()
    },
    async handleFoodRecordApi(foodRecord: IFoodRecord) {
      switch (this.foodRecordApiType) {
        case 'ADD':
          await addFoodRecordApi(foodRecord)
          ElMessage.success('新增成果成功')
          break
        case 'UPDATE':
          await updateFoodRecordApi(foodRecord.id as number, foodRecord)
          ElMessage.success('更新成果成功')
          break
        case 'DELETE':
          await deleteFoodRecordApi(foodRecord.id as number)
          ElMessage.success('删除成果成功')
          break
        default:
          break
      }
      this.foodRecordDialog.visible = false

      if (!isMobile()) {
        await this.refreshInfo()
      }
    },
    async refreshInfo() {
      await this.queryFoodRecipeList()
      await this.queryFoodRecordList()
      this.isRefresh = !this.isRefresh
    },
    getEmptyFoodRecipe(): IFoodRecipe {
      return {
        category: '',
        materialList: [],
        name: '',
        recommendRate: 0,
        remark: '',
        stepList: [],
        recordList: []
      }
    },
    getEmptyFoodMaterial(): IFoodMaterial {
      return {
        amount: '',
        name: '',
        type: ''
      }
    },
    getEmptyFoodStep(): IFoodStep {
      return {
        content: '',
        imageUrl: '',
        sort: 0
      }
    },
    getEmptyFoodRecord(): IFoodRecord {
      return {
        category: '',
        person: '',
        date: '',
        id: 0,
        imageUrl: '',
        name: ''
      }
    }
  }
})
